home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / daemons / nfs / nfs-serv.2be / nfs-serv / nfs-server-2.2beta16 / getattr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-21  |  3.2 KB  |  135 lines

  1. /*
  2.  * getattr    This module handles the NFS attributes.
  3.  *
  4.  * Authors:    Mark A. Shand, May 1988
  5.  *        Donald J. Becker, <becker@super.org>
  6.  *        Rick Sladkey, <jrs@world.std.com>
  7.  *        Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  8.  *
  9.  *        Copyright 1988 Mark A. Shand
  10.  *        This software maybe be used for any purpose provided
  11.  *        the above copyright notice is retained.  It is supplied
  12.  *        as is, with no warranty expressed or implied.
  13.  */
  14.  
  15. #include "nfsd.h"
  16.  
  17. /*
  18.  * The NFS version 2 specification fails to mention all of
  19.  * these file types, but they exist in the nfs_prot.x file.
  20.  */
  21. #define ftype_map(st_mode) (_ftype_map[((st_mode) & S_IFMT) >> 12])
  22.  
  23. ftype _ftype_map[16] =
  24. {
  25. #ifdef S_IFIFO
  26.     NFNON, NFFIFO, NFCHR, NFBAD,
  27. #else
  28.     NFNON, NFBAD, NFCHR, NFBAD,
  29. #endif
  30.     NFDIR, NFBAD, NFBLK, NFBAD,
  31.     NFREG, NFBAD, NFLNK, NFBAD,
  32.     NFSOCK, NFBAD, NFBAD, NFBAD,
  33. };
  34.  
  35. /*
  36.  * Get file attributes based on file handle
  37.  */
  38. nfsstat fh_getattr(fh, attr, stat_optimize, rqstp)
  39. nfs_fh        *fh;
  40. fattr        *attr;
  41. struct stat    *stat_optimize;
  42. struct svc_req    *rqstp;
  43. {
  44.     fhcache *fhc;
  45.  
  46.     if ((fhc = fh_find((svc_fh*)fh, FHFIND_FEXISTS)) == NULL) {
  47.         dprintf(D_CALL, "getattr: failed! No such file.\n");
  48.         return (NFSERR_STALE);
  49.     }
  50.     return fhc_getattr(fhc, attr, stat_optimize, rqstp);
  51. }
  52.  
  53. /*
  54.  * Get file attributes given the path.
  55.  */
  56. nfsstat fhc_getattr(fhc, attr, stat_optimize, rqstp)
  57. fhcache        *fhc;
  58. fattr        *attr;
  59. struct stat    *stat_optimize;
  60. struct svc_req    *rqstp;
  61. {
  62. #if DEBUG
  63.     char buff[1024];
  64.     char *sp;
  65. #endif
  66.     /* nfsstat status; */
  67.     struct stat *s;
  68.     struct stat sbuf;
  69.  
  70.     if (stat_optimize != NULL)
  71.         s = stat_optimize;
  72.     else if (lstat(fhc->path, (s = &sbuf)) != 0) {
  73.         dprintf(D_CALL, "getattr(%s): failed!  errno=%d\n", 
  74.             fhc->path, errno);
  75.         return (nfs_errno());
  76.     }
  77.     attr->type = ftype_map(s->st_mode);
  78.     attr->mode = s->st_mode;
  79.     attr->nlink = s->st_nlink;
  80.     attr->uid = ruid(s->st_uid, nfsmount, rqstp);
  81.     attr->gid = rgid(s->st_gid, nfsmount, rqstp);
  82.  
  83. #ifdef S_ISLNK
  84.     if (S_ISLNK(s->st_mode))
  85.         attr->size = NFS_MAXPATHLEN;
  86.     else
  87. #endif
  88.         attr->size = s->st_size;
  89. #ifdef HAVE_ST_BLKSIZE
  90.     attr->blocksize = s->st_blksize;
  91. #else /* !HAVE_ST_BLKSIZE */
  92. #ifdef BUFSIZ
  93.     attr->blocksize = BUFSIZ;
  94. #else /* BUFSIZ */
  95.     attr->blocksize = 1024;
  96. #endif /* !BUFSIZ */
  97. #endif /* !HAVE_ST_BLKSIZE */
  98.     attr->rdev = s->st_rdev;
  99. #ifdef HAVE_ST_BLOCKS
  100.     attr->blocks = s->st_blocks;
  101. #else
  102.     attr->blocks = st_blocks(s);
  103. #endif
  104.     attr->fsid = 1;
  105.     attr->fileid = fh_psi((nfs_fh *)&(fhc->h));
  106.     attr->atime.seconds = s->st_atime;
  107.     attr->atime.useconds = 0;
  108.     attr->mtime.seconds = s->st_mtime;
  109.     attr->mtime.useconds = 0;
  110.     attr->ctime.seconds = s->st_ctime;
  111.     attr->ctime.useconds = 0;
  112.  
  113. #ifdef DEBUG
  114.     sp = buff;
  115.     sprintf(sp, " t=%d, m=%o, lk=%d, u/g=%d/%d, sz=%d, bsz=%d",
  116.         attr->type, attr->mode, attr->nlink,
  117.         attr->uid, attr->gid, attr->size,
  118.         attr->blocksize);
  119.     sp += strlen(sp);
  120.     if (attr->type == NFCHR || attr->type == NFBLK) {
  121.         sprintf(sp, " rdev=%d/%d", (attr->rdev >> 8) & 0xff, attr->rdev & 0xff);
  122.         sp += strlen(sp);
  123.         sprintf(sp, "\n  blks=%d, fsid=%d, psi=%d, at=%d, mt=%d, ct=%d\n",
  124.             attr->blocks, attr->fsid, attr->fileid,
  125.             attr->atime.seconds,
  126.             attr->mtime.seconds,
  127.             attr->ctime.seconds);
  128.         sp += strlen(sp);
  129.     }
  130.     dprintf(D_CALL, "%s", buff);
  131. #endif
  132.  
  133.     return (NFS_OK);
  134. }
  135.